/*********************************************************************************
  *Copyright(C),2015-2020, TUYA company www.tuya.comm
  *FileName: tuya_ipc_system_control_demo.c
  *
  * File description：
  * The demo shows how the SDK uses callback to achieve system control, such as：
  * 1. Setting local ID
  * 2. Restart System and Restart Process
  * 4. Sound and LED prompts.
  *
**********************************************************************************/

#include <string.h>
#include <stdio.h>
#include "tuya_ipc_media.h"
#include "tuya_cloud_com_defs.h"
#include "tuya_cloud_types.h"
#include "tuya_ipc_common_demo.h"
#include "tuya_ipc_api.h"
#include "tuya_ipc_system_control_demo.h"
#include "InPerLed.h"
#include "InSdkSoundPlay.h"
#include "InSdkSpkCtrl.h"

#define DEF_TAG             "TYSysCtrl"

/*
Callback when the user clicks on the APP to remove the device
*/
VOID IPC_APP_Reset_System_CB(GW_RESET_TYPE_E type)
{
    printf("reset ipc success. please restart the ipc %d\n", type);
    IPC_APP_Notify_LED_Sound_Status_CB(IPC_RESET_SUCCESS);

    /* Developers need to restart IPC operations */
    Ext_InPerUartSend_Reset();
    Ext_InComFile_Creat(DEF_FLAG_RESTORE_FACTORY);
    Ext_InSdkSpkPlay_WavInit(DEF_WAV_DING_DONG);
    sleep(3);
    Ext_InSdkTcpSystem_Cmd("reboot");
}

VOID IPC_APP_Restart_Process_CB(VOID)
{
    printf("sdk internal restart request. please restart the ipc\n");
    //TODO
    /* Developers need to implement restart operations. Restart the process or restart the device. */
    sleep(1);
    Ext_InSdkTcpSystem_Cmd("reboot");
}

/* Developers need to implement the corresponding prompt sound playback and LED prompts,
   you can refer to the SDK attached files, using TUYA audio files. */
VOID IPC_APP_Notify_LED_Sound_Status_CB(IPC_APP_NOTIFY_EVENT_E notify_event)
{
    PRT_DBG(DEF_TAG,"event: %d\n", notify_event);

    switch (notify_event)
    {
    case IPC_BOOTUP_FINISH: /* Startup success */
    {
        break;
    }
    case IPC_START_WIFI_CFG: /* Start configuring the network */
    {
        break;
    }
    case IPC_START_AP_MODE: /* Start ap mode */
    {
    }
        break;

    case IPC_REV_WIFI_CFG: /* Receive network configuration information */
    {
        break;
    }
    case IPC_CONNECTING_WIFI: /* Start Connecting WIFI */
    {
    }

        break;
    case IPC_NETWORK_OK: /* Start Connecting WIFI */
    {
    }
        break;

    case IPC_MQTT_ONLINE: /* MQTT on-line */
    {
        break;
    }

    case IPC_RESET_SUCCESS: /* Reset completed */
    {
        break;
    }

    default:
        break;
    }
}

/* Callback of talkback mode,turn on or off speaker hardware*/
VOID TUYA_APP_Enable_Speaker_CB(BOOL_T enabled)
{
    //PRT_NORMAL("enable speaker: %d\n", enabled);

    if(enabled == 1)
    {
        Ext_InSdkSpkCtrl_CntAdd();
    }
    else if (enabled == 0)
    {
        Ext_InSdkSpkCtrl_CntMinus();
    }
}

/* Callback of talkback mode,turn on or off the sound */
VOID TUYA_APP_Rev_Audio_CB(IN CONST MEDIA_FRAME_S *p_audio_frame,
                           TUYA_AUDIO_SAMPLE_E audio_sample,
                           TUYA_AUDIO_DATABITS_E audio_databits,
                           TUYA_AUDIO_CHANNEL_E audio_channel)
{
    //printf("rev audio cb len:%u sample:%d db:%d channel:%d\r\n", p_audio_frame->size, audio_sample, audio_databits, audio_channel);
    //PCM-Format 8K 16Bit MONO
    //TODO
    /* Developers need to implement the operations of voice playback*/
    UINT_T size = 0;
    BYTE_T dec_buf[p_audio_frame->size * 2];

    memset(dec_buf,0,sizeof(dec_buf));
    tuya_g711_decode(TUYA_G711_MU_LAW, (PUINT16_T)p_audio_frame->p_buf,p_audio_frame->size, dec_buf, &size);
    Ext_SdkSpk_Play(dec_buf,size);
}

OPERATE_RET IPC_APP_Sync_Utc_Time(VOID)
{
    return OPRT_OK;
}

VOID IPC_APP_Show_OSD_Time(VOID)
{
}
